package me.pcqpcq.android.tvlauncher.activity;

import android.app.ProgressDialog;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import me.pcqpcq.android.tvlauncher.BaseFragmentActivity;
import me.pcqpcq.android.tvlauncher.R;
import me.pcqpcq.android.tvlauncher.adapter.AppsAdapter;
import me.pcqpcq.android.tvlauncher.entity.AppsItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 全部应用页面
 *
 * @author pcqpcq
 * @version 1.0.0
 * @since 13-11-1 下午4:48
 */
public class AllAppsActivity extends BaseFragmentActivity {

    private AppsAdapter adapter;
    private List<AppsItem> data;

    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView contentView = new ListView(this);

        data = new ArrayList<AppsItem>();

        adapter = new AppsAdapter(this, data);

        contentView.setAdapter(adapter);

        setContentView(contentView);

        new ListAppTask().execute();

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.hint_loading_apps));
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    public class ListAppTask extends AsyncTask<Void, Void, List<AppsItem>> {

        protected List<AppsItem> doInBackground(Void... args) {
            PackageManager appInfo = getPackageManager();
            List<ApplicationInfo> listInfo = appInfo.getInstalledApplications(0);
            Collections.sort(listInfo, new ApplicationInfo.DisplayNameComparator(appInfo));

            List<AppsItem> data = new ArrayList<AppsItem>();

            for (int index = 0; index < listInfo.size(); index++) {
                try {
                    ApplicationInfo content = listInfo.get(index);
                    if ((content.flags != ApplicationInfo.FLAG_SYSTEM) && content.enabled) {
                        if (content.icon != 0) {
                            AppsItem item = new AppsItem();
                            item.setName(getPackageManager().getApplicationLabel(content).toString());
                            item.setPackageName(content.packageName);
                            item.setIcon(getPackageManager().getDrawable(content.packageName, content.icon, content));
                            data.add(item);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return data;
        }

        protected void onPostExecute(List<AppsItem> result) {
            data.clear();
            data.addAll(result);
            adapter.notifyDataSetChanged();
            if (progressDialog != null) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        }
    }

}
